<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="月票名称" prop="ticketName">
        <el-input
          v-model="queryParams.ticketName"
          placeholder="请输入月票名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:parkTicketType:add']"
        >添加月票类型</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		      :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:parkTicketType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="parkTicketTypeList" border size="mini" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="月票类型id" align="center" prop="monthTicketConfigId" />
      <el-table-column label="停车场编号" align="center" prop="parkCodes" />
      <el-table-column label="月票名称" align="center" prop="ticketName" />
      <el-table-column label="月票价格" align="center" prop="price" />
      <el-table-column label="使用时长(月)" align="center" prop="duration" />
      <el-table-column label="上下架" align="center" prop="status" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value= 1
            :inactive-value= 0
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="起始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" align="center" prop="createtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:parkTicketType:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:parkTicketType:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改月票类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"   label-width="120px">
        <el-form-item label="月票名称" prop="ticketName">
          <el-input v-model="form.ticketName" placeholder="请输入月票名称" />
        </el-form-item>
        <el-form-item label="停车场编码" prop="parkCodes">
          <el-input v-model="form.parkCodes" placeholder="请输入停车场编码" />
        </el-form-item>
        <el-form-item label="使用时长(月)" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入使用时长" />
        </el-form-item>
        <el-form-item label="生效时间" prop="startTime">
          <el-date-picker clearable size="small"
            v-model="form.startTime"
            type="datetime" style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择起始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失效时间" prop="endTime">
          <el-date-picker clearable size="small"
            v-model="form.endTime"
            type="datetime" style="width: 100%"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择失效时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="月票价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入月票价格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listParkTicketType, getParkTicketType, delParkTicketType, addParkTicketType, updateParkTicketType, exportParkTicketType ,changeStatus} from "@/api/system/parkTicketType";

export default {
  name: "ParkTicketType",
  components: {
  },
  data() {
    return {
	  //按钮loading
	  buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 月票类型表格数据
      parkTicketTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态 0下架 1上架字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ticketName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ticketName:[
          {required:true,message:"请填写月票名称",trigger:"blur"}
        ],
        parkCodes:[
          {required:true,message:"请填写停车场编码",trigger:"blur"}
        ],
        startTime:[
          {required:true,message:"请选择生效时间",trigger:"blur"}
        ],
        endTime:[
          {required:true,message:"请选择时效时间",trigger:"blur"}
        ],
        price:[
          {required:true,message:"价格不能为负数或小数点后只能有两位",trigger:"blur",pattern: /^(0|[1-9]\d{0,10})(\.\d{0,2})?$/}
        ],
        duration:[
          {required:true,message:"时长为1-24个月",trigger:"blur",pattern: /^([1-9]|1[0-9]|2[0-4])$/}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("park_ticket_type_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询月票类型列表 */
    getList() {
      this.loading = true;
      listParkTicketType(this.queryParams).then(response => {
        this.parkTicketTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态 0下架 1上架字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        monthTicketConfigId: undefined,
        parkCodes: undefined,
        ticketName: undefined,
        price: undefined,
        duration: undefined,
        status: undefined,
        startTime: undefined,
        endTime: undefined,
        uuid: undefined,
        createtime: undefined,
        updatetime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 添加月票类型按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加月票类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getParkTicketType(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改月票类型";
      });
    },
    // 状态修改
    handleStatusChange(row) {
      this.$confirm('确认要月票类型id为【"' + row.monthTicketConfigId + '"】的状态吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return changeStatus(row.id, row.status);
      }).then(() => {
        this.msgSuccess("操作成功");
      }).catch(function() {
        row.status = row.status == 0 ? 1 : 0;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateParkTicketType(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (this.compareTime(this.form.startTime,this.form.endTime) == false){
              this.msgError("请选择正确的时间段,失效时间不能小于生效时间!");
              return false;
            }
            addParkTicketType(this.form).then(response => {
              this.buttonLoading = false;
              this.msgSuccess("添加月票类型成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除月票类型编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delParkTicketType(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有月票类型数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportParkTicketType(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
